package com.pirestupppai.widgets.dialog;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseDialog;
import com.pirestupppai.bean.LoginUser;
import com.pirestupppai.bean.TupaiUser;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.ui.activity.EditInfoActivity;

import butterknife.BindView;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

/**
 *  用户个人中心 Dialog
 */

public class UserSelectMoreDialog extends BaseDialog implements View.OnClickListener {

    private Context mContext;
    private int mUid;

    @BindView(R.id.user_select)
    TextView mUserSelectTv;
    @BindView(R.id.cancel_btn)
    TextView mCancelTv;

    public UserSelectMoreDialog(Context context, int uid) {
        super(context, R.style.CustomDialogStyle);
        this.mContext = context;
        this.mUid = uid;
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.dialog_user_select_more;
    }

    @Override
    protected boolean atBottom() {
        return true;
    }

    @Override
    protected void initViews() {
        if (mUid == LoginUser.getInstance().getId()) {
            mUserSelectTv.setText("编辑个人信息");
        } else {
            mUserSelectTv.setText("发消息");
        }
    }

    @Override
    protected void initListeners() {
        mUserSelectTv.setOnClickListener(this);
        mCancelTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_select:
                if (mUid == LoginUser.getInstance().getId()) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, EditInfoActivity.class);
                    mContext.startActivity(intent);
                } else {
                    getUserDataAndSendMessage();
                }
                dismiss();
                break;
            case R.id.cancel_btn:
                dismiss();
                break;
        }
    }

    private void getUserDataAndSendMessage() {
        NetworkRequest.getInstance().getUserData(mUid, mContext, new TupaiSubscriber<TupaiUser>(mContext) {
            @Override
            protected void requestSuccess(TupaiUser user) {
                final String id = Integer.toString(user.getId());
                final String nickname = user.getNickname();
                final Uri avatar = Uri.parse(user.getAvatar() + "?imageView2/2/w/200/h/200");
                RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
                    @Override
                    public UserInfo getUserInfo(String s) {
                        return new UserInfo(id, nickname, avatar);
                    }
                }, true);
                RongIM.getInstance().refreshUserInfoCache(new UserInfo(id, nickname, avatar));
                RongIM.getInstance().startPrivateChat(mContext, id, nickname);
            }

            @Override
            protected void requestError(String error) {

            }
        });
    }
}
